SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_DeleteTab] 
	@TabId int
AS
	BEGIN
		DECLARE @TabOrder INT
		SET @TabOrder = (SELECT TabOrder FROM DnnProduct_Tabs WHERE TabID = @TabId)

		DECLARE @ParentID INT
		SET @ParentID = (SELECT ParentID FROM DnnProduct_Tabs WHERE TabID = @TabId)

		DECLARE @ContentItemID INT
		SET @ContentItemID = (SELECT ContentItemID FROM DnnProduct_Tabs WHERE TabID = @TabId)

		-- Delete Tab --
		DELETE FROM DnnProduct_Tabs
			WHERE  TabId = @TabId

		-- Update TabOrder of remaining Tabs --
		UPDATE DnnProduct_Tabs
			SET TabOrder = TabOrder - 2
			WHERE (ParentId = @ParentId OR (ParentId IS NULL AND @ParentID IS NULL))
				AND TabOrder > @TabOrder
			
		-- Delete Content Item --
		DELETE DnnProduct_ContentItems
			WHERE ContentItemID = @ContentItemID
	END

GO
